<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of EventController
 *
 * @author tuancasi
 */
class EventController extends Controller {
    /*
     * get more event when user scroll
     */

    public function actionMore_event() {

	if (isset($_POST['day']) && isset($_POST['month']) && isset($_POST['year']) && isset($_POST['currPage'])) {
	    $nextPage = intval($_POST['currPage']) * Event::PAGE_SIZE;

	    //if user pick date, get event by date
	    if ($_POST['day']) {
		$modelEvent = Event::model()->findAll(array(
		    'condition' => 'DAY(start)=' . intval($_POST['day']) . ' and MONTH(start)=' . intval($_POST['month']) . ' and YEAR(start)=' . intval($_POST['year']) . ' and status=' . Event::STATUS_APPROVE,
		    "order" => "id DESC",
		    "offset" => $nextPage,
		    "limit" => Event::PAGE_SIZE
		));
	    } else {
		//get all event 
		$modelEvent = Event::model()->findAll(array(
		    'condition' => 'status=' . Event::STATUS_APPROVE,
		    "order" => "id DESC",
		    "offset" => $nextPage,
		    "limit" => Event::PAGE_SIZE
		));
	    }
	    if ($modelEvent) {
		foreach ($modelEvent as $event) {
		    echo '<li>';
		    echo '<div class="event-ca">';
		    echo '<span class="day">' . date('M', strtotime($event->start)) . '</span>';
		    echo date('j', strtotime($event->start));
		    echo '</div>';
		    echo '<div class="event-text">';
		    echo '<h3><a href="' . SITE_NAME_URL . 'event/' . $event->id . '">' . h($event->title, 25) . '</a></h3>';
		    echo '<p class="word-break">' . h($event->description, 66) . '</p>';
		    echo '</div>';
		    echo '</li>';
		}
	    }
	}
    }

    /**
     * get event when user click on calendar
     */
    public function actionGet_event_by_click() {
	if (isset($_POST['day']) && isset($_POST['month']) && isset($_POST['year'])) {

	    $modelEvent = Event::model()->findAll(array(
		'condition' => 'DAY(start)=' . intval($_POST['day']) . ' and MONTH(start)=' . intval($_POST['month']) . ' and YEAR(start)=' . intval($_POST['year']),
		'order' => 'id DESC',
		'limit' => Event::PAGE_SIZE
	    ));
	    if ($modelEvent) {
		foreach ($modelEvent as $event) {
		    echo '<li>';
		    echo '<div class="event-ca">';
		    echo '<span class="day">' . date('M', strtotime($event->start)) . '</span>';
		    echo date('j', strtotime($event->start));
		    echo '</div>';
		    echo '<div class="event-text">';
		    echo '<h3><a href="' . SITE_NAME_URL . 'event/' . $event->id . '">' . h($event->title, 25) . '</a></h3>';
		    echo '<p class="word-break">' . h($event->description, 46) . '</p>';
		    echo '</div>';
		    echo '</li>';
		}
	    } else {
		echo "<li>There is no event at that time!</li>";
	    }
	}
    }

    /*
     * view one event
     */

    public function actionView($id) {
	$modelEvent = Event::model()->findByPk($id);
	if ($modelEvent) {
	    
	} else {
	    throw new CHttpException(404, 'Page was not exists');
	}
	$this->render('view', array("model" => $modelEvent));
    }

}
